package com.longge;

public class IslandPerimeter {


    public int islandPerimeter(int[][] grid) {
        int[] dx = {0, 1, 0, -1};
        int[] dy = {1, 0, -1, 0};
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {

                if (grid[i][j] == 1) {
                    for (int k = 0; k < 4; k++) {
                        int x = dx[k] + i;
                        int y = dy[k] + j;
                        if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) {
                            count++;
                        }else if (grid[x][y] == 0) {
                            count++;
                        }
                    }
                }
            }
        }
        return count;

    }
}
